//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BankruptcyEvent.h"
using namespace Cephei::QL::Experimental::Credit;
#include <gen/QL/Currency.h>
#include <gen/QL/Experimental/Credit/DefaultType.h>
#include <gen/QL/Experimental/Credit/DefaultProbKey.h>
#include <gen/QL/Experimental/Credit/DefaultEvent.h>
using namespace Cephei::QL;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Credit::CBankruptcyEvent::CBankruptcyEvent (DateTime creditEventDate, Cephei::QL::ICurrency^ curr, QL::Experimental::Credit::SeniorityEnum bondsSen, DateTime settleDate, Double recoveryRates) : CDefaultEvent(CBankruptcyEvent::typeid)
{
    CCurrency^ _Ccurr;
    try
    {
#ifdef HANDLE
        _phBankruptcyEvent = NULL;
#endif
        QuantLib::Date _creditEventDate = (QuantLib::Date)ValueHelper::Convert (creditEventDate); //d
        _Ccurr = safe_cast<CCurrency^> (curr);
        _Ccurr->Lock();
        QuantLib::Currency& _curr = static_cast<QuantLib::Currency&> (_Ccurr->GetReference ()); 
        QuantLib::Seniority _bondsSen = (QuantLib::Seniority)bondsSen ;
        QuantLib::Date _settleDate = (QuantLib::Date)ValueHelper::Convert (settleDate); //d
        QuantLib::Real _recoveryRates = (QuantLib::Real)ValueHelper::Convert (recoveryRates); //d
        _ppBankruptcyEvent = new boost::shared_ptr<QuantLib::BankruptcyEvent> (new QuantLib::BankruptcyEvent ( _creditEventDate,  _curr,  _bondsSen,  _settleDate,  _recoveryRates ));
        SetDefaultEvent (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppBankruptcyEvent));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccurr != nullptr) _Ccurr->Unlock();
    }
}
Cephei::QL::Experimental::Credit::CBankruptcyEvent::CBankruptcyEvent (boost::shared_ptr<QuantLib::BankruptcyEvent>& childNative, Object^ owner) : CDefaultEvent(CBankruptcyEvent::typeid)
{
#ifdef HANDLE
	_phBankruptcyEvent = NULL;
#endif
	_ppBankruptcyEvent = &childNative;
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppBankruptcyEvent));
}
Cephei::QL::Experimental::Credit::CBankruptcyEvent::CBankruptcyEvent (QuantLib::BankruptcyEvent& childNative, Object^ owner) : CDefaultEvent(CBankruptcyEvent::typeid)
{
#ifdef HANDLE
	_phBankruptcyEvent = NULL;
#endif
	_ppBankruptcyEvent = new boost::shared_ptr<QuantLib::BankruptcyEvent> (&childNative);
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppBankruptcyEvent));
    _BankruptcyEventOwner = owner;
    _DefaultEventOwner = owner;
}

Cephei::QL::Experimental::Credit::CBankruptcyEvent::CBankruptcyEvent (CBankruptcyEvent^ copy) : CDefaultEvent(CBankruptcyEvent::typeid)
{
#ifdef HANDLE
	_phBankruptcyEvent = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBankruptcyEvent = new boost::shared_ptr<QuantLib::BankruptcyEvent> (copy->GetShared());
        _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppBankruptcyEvent));
    }
}
Cephei::QL::Experimental::Credit::CBankruptcyEvent::CBankruptcyEvent (PLATFORM::Type^ t) : CDefaultEvent(CBankruptcyEvent::typeid)
{
#ifdef HANDLE
	_phBankruptcyEvent = NULL;
#endif
	if (!t->IsSubclassOf(CBankruptcyEvent::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Credit::CBankruptcyEvent::CBankruptcyEvent (QuantLib::Handle<QuantLib::BankruptcyEvent>& childNative, Object^ owner)  : CDefaultEvent(CBankruptcyEvent::typeid)
{
	_phBankruptcyEvent = &childNative;
	_ppBankruptcyEvent = &static_cast<boost::shared_ptr<QuantLib::BankruptcyEvent>>(childNative.currentLink());
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppBankruptcyEvent));
    _BankruptcyEventOwner = owner;
}
Cephei::QL::Experimental::Credit::CBankruptcyEvent::CBankruptcyEvent (QuantLib::Handle<QuantLib::BankruptcyEvent> childNative)  : CDefaultEvent(CBankruptcyEvent::typeid)
{
	_phBankruptcyEvent = &childNative;
	_ppBankruptcyEvent = &static_cast<boost::shared_ptr<QuantLib::BankruptcyEvent>>(childNative.currentLink());
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppBankruptcyEvent));
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Credit::CBankruptcyEvent::CBankruptcyEvent (QuantLib::BankruptcyEvent childNative)  : CDefaultEvent(CBankruptcyEvent::typeid)
{
#ifdef HANDLE
	_phBankruptcyEvent = NULL;
#endif
	_ppBankruptcyEvent = new boost::shared_ptr<QuantLib::BankruptcyEvent> (new QuantLib::BankruptcyEvent (childNative));
    _ppDefaultEvent = new boost::shared_ptr<QuantLib::DefaultEvent> (boost::dynamic_pointer_cast<QuantLib::DefaultEvent> (*_ppBankruptcyEvent));
}
#endif

Cephei::QL::Experimental::Credit::CBankruptcyEvent::~CBankruptcyEvent ()
{
    if (_ppBankruptcyEvent != NULL)
    {
	    delete _ppBankruptcyEvent;
        _ppBankruptcyEvent = NULL;
    }
}
Cephei::QL::Experimental::Credit::CBankruptcyEvent::!CBankruptcyEvent ()
{
    if (_ppBankruptcyEvent != NULL)
    {
	    delete _ppBankruptcyEvent;
    }
}
QuantLib::BankruptcyEvent& Cephei::QL::Experimental::Credit::CBankruptcyEvent::GetReference ()
{
    if (_ppBankruptcyEvent == NULL) throw REFNEW NativeNullException ();
	return **_ppBankruptcyEvent;
}
boost::shared_ptr<QuantLib::BankruptcyEvent>& Cephei::QL::Experimental::Credit::CBankruptcyEvent::GetShared ()
{
    if (_ppBankruptcyEvent == NULL) throw REFNEW NativeNullException ();
	return *_ppBankruptcyEvent;
}
QuantLib::BankruptcyEvent* Cephei::QL::Experimental::Credit::CBankruptcyEvent::GetPointer ()
{
    if (_ppBankruptcyEvent == NULL) throw REFNEW NativeNullException ();
	return &**_ppBankruptcyEvent;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BankruptcyEvent>& Cephei::QL::Experimental::Credit::CBankruptcyEvent::GetHandle ()
{
	if (_phBankruptcyEvent == NULL)
	{
		_phBankruptcyEvent = new Handle<QuantLib::BankruptcyEvent> (*_ppBankruptcyEvent);
	}
	return *_phBankruptcyEvent;
}
#endif
bool Cephei::QL::Experimental::Credit::CBankruptcyEvent::HasNative () 
{
	return (_ppBankruptcyEvent != NULL);
}

Boolean Cephei::QL::Experimental::Credit::CBankruptcyEvent::MatchesEventType (Cephei::QL::Experimental::Credit::IDefaultType^ prm1)
{
    CDefaultType^ _Cprm1;
    try
    {
        _Cprm1 = safe_cast<CDefaultType^> (prm1);
        _Cprm1->Lock();
        boost::shared_ptr<QuantLib::DefaultType>& _prm1 = static_cast<boost::shared_ptr<QuantLib::DefaultType>&> (_Cprm1->GetShared ()); 
    	bool _rv = (bool)(*_ppBankruptcyEvent)->matchesEventType ( _prm1 );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprm1 != nullptr) _Cprm1->Unlock();
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Credit::IBankruptcyEvent^ Cephei::QL::Experimental::Credit::CBankruptcyEvent_Factory::Create (DateTime creditEventDate, Cephei::QL::ICurrency^ curr, QL::Experimental::Credit::SeniorityEnum bondsSen, DateTime settleDate, Double recoveryRates)
{
    return REFNEW CBankruptcyEvent ( creditEventDate,  curr,  bondsSen,  settleDate,  recoveryRates);
}
